#ifndef TT_AVCALL_IMPL_HXX
#define TT_AVCALL_IMPL_HXX
#pragma once

#include <deque>
#include <map>
#include <set>
#include <string>

#include "Call.h"
#include "CallFactory.h"
#include "VideoRendererAdapter.h"
#include "talk/app/webrtc/mediastreaminterface.h"
#include "talk/app/webrtc/peerconnectioninterface.h"
#include "webrtc/base/scoped_ptr.h"


const char kAudioLabel[] = "audio_label";
const char kVideoLabel[] = "video_label";
const char kStreamLabel[] = "stream_label";

namespace webrtc {
class VideoCaptureModule;
}  // namespace webrtc

namespace cricket {
class VideoRenderer;
}  // namespace cricket

class CallImpl : public Call,
    public webrtc::PeerConnectionObserver,
    public webrtc::CreateSessionDescriptionObserver
{
 public:
	 CallImpl(Direction dir,const IceServers& iceServers);

  virtual void SetCallEventObserver(CallEventObserver *observer);

 protected:
  ~CallImpl();
  bool InitializePeerConnection();

  bool CreatePeerConnection(bool dtls);

  void DeletePeerConnection();

  void AddLocalStreams();

  cricket::VideoCapturer* OpenVideoCaptureDevice();

  // PeerConnectionObserver implementation.
  virtual void OnStateChange( webrtc::PeerConnectionObserver::StateType state_changed) {}
  
  virtual void OnAddStream(webrtc::MediaStreamInterface* stream);
  
  virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream);
  
  virtual void OnDataChannel(webrtc::DataChannelInterface* channel) {}
 
  virtual void OnRenegotiationNeeded() {}
  
  virtual void OnIceChange() {}
  
  virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate);
  
  virtual void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state);

  // CreateSessionDescriptionObserver implementation.
  virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc);

  virtual void OnFailure(const std::string& error);

  virtual void SetRemoteSDP(const char *sdp,int len);

  virtual void CreatePeerConnection();

  virtual void DisconnectPeerConnection();

  virtual void StartLocalRenderer(rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video);

  virtual void StopLocalRenderer();

  virtual void StartRemoteRenderer(rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video);

  virtual void StopRemoteRenderer();

  virtual void RegisterVideoRenderer(VideoRenderer *local_renderer, VideoRenderer *remote_renderer, VideoFrameType frame_type);

  virtual Direction dir() override;

  virtual int AddRef(){ return -1; }
  virtual int Release(){ return -1; }

 protected:
  bool loopback_;
  rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
  rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory_;
  std::map<std::string, rtc::scoped_refptr<webrtc::MediaStreamInterface>> active_streams_;
  rtc::scoped_ptr<VideoRendererAdapter> local_renderer_adapter_;
  rtc::scoped_ptr<VideoRendererAdapter> remote_renderer_adapter_;
  VideoRenderer *local_renderer_;
  VideoFrameType local_renderer_frame_type_;
  VideoRenderer *remote_renderer_;
  VideoFrameType remote_renderer_frame_type_;
  Direction dir_;
  CallEventObserver *observer_;
  std::string local_sdp_;
  const IceServers& iceServers_;
};

#endif  // TALK_EXAMPLES_PEERCONNECTION_CLIENT_CallImpl_H_


